package com.utils
{
	import flash.events.KeyboardEvent;
	
	import mx.controls.Alert;
	
	import spark.components.List;
	
	public class CustomList extends List
	{
		public function CustomList()
		{
			super();
			addEventListener(KeyboardEvent.KEY_DOWN, interceptKey);
		}
		
		private function findItem( searchString:String, start:int ): int {
			var pattern:RegExp = new RegExp("^"+searchString, "i");
			var current:int = 0;
			for each( var item:Object in this.dataProvider ) {
				if( item.name.search(pattern) >= 0 && current>start){
					return current;
				}
				current++;
			}
			current = 0;
			for each( var item2:Object in this.dataProvider ) {
				if( item2.name.search(pattern) >= 0 && current!=start){
					return current;
				}
				current++;
			}
			
			return -1;
		}
		
		protected function interceptKey(event:KeyboardEvent):void
		{
			//only letters and numbers allowed
			if ((event.keyCode >= 65 && event.keyCode<=90) || 
				(event.keyCode >= 48 && event.keyCode<=57) ||
				(event.keyCode >= 100 && event.keyCode<=122)
			){
				var itemIndex:int = findItem(String.fromCharCode(event.charCode), this.selectedIndex);
				if (itemIndex != -1){
					this.selectedIndex = itemIndex;
					this.ensureIndexIsVisible(itemIndex);
				}
			}
			
			
		}
	}
}